package com.cl.unipay4j.ali.splitbill;

import com.alibaba.fastjson.JSON;
import com.alipay.api.request.AlipayTradeRoyaltyRelationUnbindRequest;
import com.alipay.api.response.AlipayTradeRoyaltyRelationUnbindResponse;
import com.cl.unipay4j.ali.AbstractAliPay;
import com.cl.unipay4j.core.Result;
import com.cl.unipay4j.core.splitbill.ISplitBillRelationUnbind;
import com.cl.unipay4j.core.splitbill.UnBindModel;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 支付宝分账关系解绑
 *
 * @author siashan
 * @since v1.0.1
 **/
public class AliPaySplitBillRelationUnBind extends AbstractAliPay implements ISplitBillRelationUnbind {
    /**
     * 分账关系解绑
     *
     * @param model       请求参数
     * @return 解绑结果
     */
    @Override
    public Result unbind(UnBindModel model) {
        try {
            AlipayTradeRoyaltyRelationUnbindRequest request = new AlipayTradeRoyaltyRelationUnbindRequest();
            request.setBizContent(JSON.toJSONString(wrapParam(model)));
            AlipayTradeRoyaltyRelationUnbindResponse response = alipayClient.execute(request);
            if (response.isSuccess()) {
                System.out.println("调用成功");
            } else {
                System.out.println("调用失败");
            }

        } catch (Exception e) {

        }
        return Result.exception();
    }

    /**
     * 封装请求参数
     *
     * @param model  请求实体
     * @return  请求参数
     */
    private Map<String, String> wrapParam(UnBindModel model) {
        Map<String, String> map = new HashMap<>();
        List<Map<String, String>> list = new ArrayList<>();

        Map<String, String> receiver = new HashMap<>();
        receiver.put("type", model.getType());
        receiver.put("account", model.getAccount());
        receiver.put("name", model.getName());
        receiver.put("memo", model.getCustom_relation());

        list.add(receiver);
        map.put("receiver_list", JSON.toJSONString(list));
        map.put("out_request_no", model.getOutTradeNo());

        return map;
    }
}
